package pers.cz.controller;

import pers.cz.exception.ErrorCode;
import pers.cz.exception.BusinessException;
import pers.cz.model.TaskJob;
import pers.cz.pub.RespBean;
import pers.cz.service.TaskJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: TaskJobController
 * @author: Cheng Zhi
 * @create: 2021-04-26 20:30
 **/
@Api(tags = "定时任务接口")
@RestController
@RequestMapping(path = "/api/taskJob")
public class TaskJobController {

    @Autowired
    TaskJobService service;

    @ApiOperation(value = "添加任务调度")
    @PostMapping(path = "/addTask")
    public RespBean addTask(@RequestBody Map map, HttpServletRequest request)  {

        if (map.get("taskName") == null || map.get("cron") == null || map.get("testCaseIds") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        String taskName = map.get("taskName").toString();
        String cron = map.get("cron").toString();
        String testCaseIds = map.get("testCaseIds").toString();
        String testCaseType = map.get("testCaseType").toString();

        // 超时时间,用于进度条展示
        String ext1 = map.get("ext1").toString();
        // 测试环境编号
        String ext2 = map.get("ext2").toString();

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        int count = service.addTask(taskName, cron, testCaseIds, ext1, ext2, userId, testCaseType);

        return RespBean.ok("成功添加[" + count + "]条");
    }

    @ApiOperation(value = "根据任务名称查询任务")
    @PostMapping(path = "/queryTaskJobByJobName")
    public RespBean queryTaskJobByJobName(@RequestBody Map map) {

        if (map.get("name") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        String name = map.get("name").toString();
        List<TaskJob> taskJobs = service.queryTaskJobByJobName(name);
        return RespBean.ok("", taskJobs);
    }

    @ApiOperation(value = "分页查询所有任务")
    @PostMapping(path = "/queryAllTaskJobByPage")
    public List<TaskJob> queryAllTaskJobByPage(@RequestBody Map map) {

        if (map.get("pageStart") == null || map.get("pageLimit") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        List<TaskJob> taskJobs = service.queryAllTaskJobByPage(pageStart, pageLimit);
        return taskJobs;
    }

    @ApiOperation(value = "查询任务总数")
    @GetMapping(path = "/quertCountTaskJob")
    public Integer quertCountTaskJob() {

        int count = service.quertCountTaskJob();

        return count;
    }

    @ApiOperation(value = "修改任务开关状态")
    @PostMapping(path = "/updateTaskJobSwitch")
    public Integer updateTaskJobSwitch(@RequestBody Map map) {

        if (map.get("id") == null || map.get("jobWitch") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        Integer id = (Integer) map.get("id");
        Boolean jobWitch = (Boolean) map.get("jobWitch");
        int count = service.updateTaskJobSwitch(id, jobWitch);
        return count;
    }

    @ApiOperation(value = "删除指定id任务")
    @PostMapping(path = "/deleteJob")
    public Integer deleteJob(@RequestBody Map map) {

        if (map.get("id") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        Integer id = (Integer) map.get("id");
        int count = service.deleteJob(id);
        return count;
    }

    @ApiOperation(value = "修改任务cron表达式")
    @PostMapping(path = "/updateTaskJobCron")
    public Integer updateTaskJobCron(@RequestBody Map map) {

        if (map.get("id") == null || map.get("jobCron") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        Integer id = (Integer) map.get("id");
        String cron = (String) map.get("jobCron");
        String jobName = (String) map.get("jobName");
        int count = service.updateTaskJobCron(id, cron, jobName);
        return count;
    }


    @ApiOperation(value = "立即启动定时任务")
    @PostMapping(path = "/startTaskJob")
    public RespBean stratTaskJob(@RequestBody Map map) {

        Integer id = (Integer) map.get("id");
        // 直接执行
        int i = service.startJobNow(id);
        return RespBean.ok("启动成功");
    }


}
